package com.lixixi.tingshu.account.listener;

import com.alibaba.fastjson.JSONObject;
import com.lixixi.tingshu.account.service.UserAccountService;
import com.lixixi.tingshu.common.constant.KafkaConstant;
import com.lixixi.tingshu.model.account.UserAccount;
import com.lixixi.tingshu.vo.account.AccountLockVo;
import com.lixixi.tingshu.vo.user.UserPaidRecordVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Map;

@Slf4j
@Component
public class AccountListener {
	@Autowired
	UserAccountService userAccountService;

	@Autowired
	KafkaTemplate<String, String> kafkaTemplate;

	/**
	 * 监听用户注册消息,接收用户id,初始化用户账户
	 * @param userId
	 */
	@KafkaListener(id = "userAccountListener", topics = KafkaConstant.QUEUE_USER_REGISTER)
	void userAccountInitByListen(String userId){
		log.info("监听中====>用户id:{}=>初始化账户信息", userId);
		UserAccount userAccount = new UserAccount();
		userAccount.setUserId(Long.parseLong(userId));
		userAccountService.save(userAccount);
	}

	@KafkaListener(id = "userAccountMinusByListen", topics = KafkaConstant.QUEUE_ACCOUNT_MINUS)
	void userAccountMinusByListen(String orderNo){
		userAccountService.minus(orderNo);
	}

	void userAccountUnLockByListen(String accountLockStr){
		AccountLockVo accountLockVo = JSONObject.parseObject(accountLockStr, AccountLockVo.class);
		userAccountService.unlock(accountLockVo);
	}
}
